home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / proff.zip / LOOKUP.C < prev    next >
Text File  |  1988-02-12  |  3KB  |  164 lines

  1. /*
  2.  * from K&R "The C Programming language"
  3.  * Table lookup routines
  4.  *
  5.  */
  6. #include <stdio.h>
  7. #include "proff.h"
  8. /*
  9.  * hash - for a hash value for string s
  10.  *
  11.  */
  12. hash(s)
  13. char *s;
  14. {
  15.     int    hashval;
  16.  
  17.     for (hashval = 0; *s != '\0';)
  18.         hashval += *s++;
  19.     return (hashval % HASHMAX);
  20. }
  21.  
  22. /*
  23.  * lookup - lookup for a string s in the hash table
  24.  *
  25.  */
  26. struct hashlist
  27. *lookup(s, hashtab)
  28. char *s;
  29. struct hashlist *hashtab[];
  30. {
  31.     struct hashlist *np;
  32.  
  33.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  34.         if (strcmp(s, np->name) == 0)
  35.             return(np);    /* found     */
  36.     return(NULL);        /* not found */
  37. }
  38.  
  39. /*
  40.  * install - install a string name in hashtable and its value def
  41.  * at a given hashtable.
  42.  */
  43. struct hashlist
  44. *install(name,def,hashtab)
  45. char *name;
  46. char *def;
  47. struct hashlist *hashtab[];
  48. {
  49.     int hashval;
  50.     struct hashlist *np, *lookup();
  51.     char *strsave(), *malloc();
  52.  
  53.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  54.         np = (struct hashlist *) malloc(sizeof(*np));
  55.         p_memoryus += sizeof(*np);
  56.         if (np == NULL)
  57.             return(NULL);
  58.         if ((np->name = strsave(name)) == NULL)
  59.             return(NULL);
  60.         hashval = hash(np->name);
  61.         np->next = hashtab[hashval];
  62.         hashtab[hashval] = np;
  63.     } else                    /* found..     */
  64.         free(np->def);            /* free prev.  */
  65.     if ((np->def = strsave(def)) == NULL)
  66.         return(NULL);
  67.     return(np);
  68. }
  69.  
  70. /*
  71.  * strsave - save string s somewhere
  72.  *
  73.  */
  74. char
  75. *strsave(s)
  76. char *s;
  77. {
  78.     char *p, *malloc();
  79.     register int n;
  80.  
  81.     n = strlen(s) + 1;
  82.     if ((p = malloc(n)) != NULL) {
  83.         p_memoryus += n;
  84.         strcpy(p, s);
  85.     }
  86.     return(p);
  87. }
  88.  
  89. /*
  90.  * lexinstal - instal a string name in hashtable and its value
  91.  *           used by lexical analyser to quickly match a token
  92.  *           and return its lexical value.
  93.  *
  94.  */
  95. struct lexlist
  96. *lexinstal(name,val,flag,lextable)
  97. char *name;
  98. int val;
  99. int flag;
  100. struct lexlist *lextable[];
  101. {
  102.     int hashval;
  103.     struct lexlist *np,*lexlook();
  104.     char *strsave(), *malloc();
  105.  
  106.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  107.         np = (struct lexlist *) malloc(sizeof(*np));
  108.         p_memoryus += sizeof(*np);
  109.         if (np == NULL)
  110.             return(NULL);
  111.         if ((np->name = strsave(name)) == NULL)
  112.             return(NULL);
  113.         hashval = hash(np->name);
  114.         np->link = lextable[hashval];
  115.         lextable[hashval] = np;
  116.     }
  117.     np->val = val;                /* replace prev */
  118.     np->flag = flag;
  119.     return(np);
  120. }
  121.  
  122. /*
  123.  * lexlook - lookup for a string s in the hash table
  124.  *         used by lexinstal only.
  125.  *
  126.  */
  127. struct lexlist
  128. *lexlook(s,table)
  129. char *s;
  130. struct lexlist *table[];
  131. {
  132.     struct lexlist *np;
  133.  
  134.     for (np = table[hash(s)]; np != NULL; np = np->link)
  135.         if (strcmp(s, np->name) == 0)
  136.             return(np);    /* found     */
  137.     return(NULL);        /* not found */
  138. }
  139.  
  140. /*
  141.  * remove an item from the hash table forever
  142.  *
  143.  */
  144. struct lexlist
  145. *xremove(s, table)
  146. char *s;
  147. struct lexlist *table[];
  148. {
  149.     struct lexlist *np, *xp;
  150.  
  151.     np = table[hash(s)];
  152.     xp = np; 
  153.     while (np != NULL) {
  154.         if (strcmp(s, np->name) == 0) {
  155.             xp->link = np->link;    /* remove the link */
  156.             return(np);        /* return the lost */
  157.         }
  158.         xp = np; 
  159.         np = np->link;
  160.     }
  161.     return(NULL);
  162. }
  163.  
  164.